package org.example.erat.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.erat.entity.ClassEntity;
import org.example.erat.entity.Course;
import org.example.erat.entity.Experiment;
import org.example.erat.entity.Student;
import org.example.erat.repository.ClassRepository;
import org.example.erat.repository.CourseRepository;
import org.example.erat.repository.ExperimentRepository;
import org.example.erat.repository.StudentRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据初始化配置类
 */
@Configuration
@RequiredArgsConstructor
@Slf4j
public class DataInitializer {

    /**
     * 初始化测试数据
     * @param courseRepository 课程仓库
     * @param classRepository 班级仓库
     * @param experimentRepository 实验仓库
     * @param studentRepository 学生仓库
     * @return 命令行运行器
     */
    @Bean
    @Profile("!prod") // 非生产环境才初始化测试数据
    public CommandLineRunner initData(
            CourseRepository courseRepository,
            ClassRepository classRepository,
            ExperimentRepository experimentRepository,
            StudentRepository studentRepository) {
        return args -> {
            log.info("初始化测试数据...");

            // 检查是否已有数据
            if (courseRepository.count() > 0) {
                log.info("数据库中已有数据，跳过初始化");
                return;
            }

            // 创建课程
            List<Course> courses = new ArrayList<>();
            courses.add(Course.builder()
                    .name("数据结构")
                    .teacher("张教授")
                    .experimentCount(5)
                    .build());
            courses.add(Course.builder()
                    .name("软件工程")
                    .teacher("李教授")
                    .experimentCount(8)
                    .build());
            courses.add(Course.builder()
                    .name("机器学习")
                    .teacher("王教授")
                    .experimentCount(6)
                    .build());
            courseRepository.saveAll(courses);
            log.info("创建了 {} 个课程", courses.size());

            // 创建班级
            List<ClassEntity> classes = new ArrayList<>();
            classes.add(ClassEntity.builder()
                    .name("计算机科学1班")
                    .course(courses.get(0))
                    .build());
            classes.add(ClassEntity.builder()
                    .name("软件工程2班")
                    .course(courses.get(1))
                    .build());
            classes.add(ClassEntity.builder()
                    .name("人工智能3班")
                    .course(courses.get(2))
                    .build());
            classRepository.saveAll(classes);
            log.info("创建了 {} 个班级", classes.size());

            // 创建实验
            List<Experiment> experiments = new ArrayList<>();
            for (Course course : courses) {
                for (int i = 1; i <= course.getExperimentCount(); i++) {
                    experiments.add(Experiment.builder()
                            .name("实验" + i)
                            .experimentNumber(i)
                            .course(course)
                            .build());
                }
            }
            experimentRepository.saveAll(experiments);
            log.info("创建了 {} 个实验", experiments.size());

            // 创建学生
            List<Student> students = new ArrayList<>();
            for (int i = 0; i < classes.size(); i++) {
                ClassEntity classEntity = classes.get(i);
                for (int j = 1; j <= 10; j++) {
                    String studentId = "2023" + String.format("%02d", i + 1) + String.format("%02d", j);
                    students.add(Student.builder()
                            .studentId(studentId)
                            .name("学生" + studentId)
                            .grade("2023")
                            .major("计算机科学与技术")
                            .classEntity(classEntity)
                            .build());
                }
            }
            studentRepository.saveAll(students);
            log.info("创建了 {} 个学生", students.size());

            log.info("测试数据初始化完成");
        };
    }
}
